#include <math.h>

#include"GlobalDef.h"
#include"DEMOGameState_Context.h"
#include"DEMOInputMng.h"
#include"DEMOConsoleLog.h"
#include"DEMORenderComponentManger.h"

#include"DEMOInput_Helper.h"

//#include"AuroraSprite.h"			//	test AuroraSupport

HGE								*hge = NULL;
DEMOGameState_Context			*g_GameState = NULL;				//	manage game state
DEMOInputManager				*g_InputMng = NULL;					//	manage input
DEMORenderComponentManger				*g_RenderMng = NULL;					//	manage actor

bool InitGame()
{
	g_GameState = new DEMOGameState_Context();
	g_InputMng = new DEMOInputManager();

	extern bool ProcessInput_Helper(hgeInputEvent theEvent);
	g_InputMng->SetKeyEventDelegate(DEMOInputHelper::ProcessInput_Helper);

	g_RenderMng = DEMORenderComponentManger::getRenderComponentManager();

	return false;
}

bool CloseGame()
{
	if( g_GameState != NULL)
	{
		delete g_GameState;
		g_GameState = NULL;
	}

	if ( g_InputMng!= NULL)
	{
		delete g_InputMng;
		g_InputMng = NULL;
	}

	DEMORenderComponentManger::gc();

	return false;
}


bool FrameFunc()
{
	float deltaTime = hge->Timer_GetDelta();
	g_GameState->Tick(deltaTime);
	g_InputMng->ProcessInput();

	g_RenderMng->tick(deltaTime);

	return false;
}

bool RenderFunc()
{
	hge->Gfx_BeginScene();
	//hge->Gfx_Clear( 0xFFFFFFFF );
	hge->Gfx_Clear( 0 );

	g_GameState->Render();
	g_RenderMng->render();

	hge->Gfx_EndScene();

	return false;
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	DEMOConsoleLog consoleLog;

	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "DemoGame.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "DemoGame");
	hge->System_SetState(HGE_WINDOWED, bWindowed);
	hge->System_SetState(HGE_SCREENWIDTH, SCREEN_WIDTH);
	hge->System_SetState(HGE_SCREENHEIGHT, SCREEN_HEIGHT);
	hge->System_SetState(HGE_SCREENBPP, 32);

	if(hge->System_Initiate())
	{
		InitGame();


		/*AuroraSprite* pAuroraSprite = new AuroraSprite();
		pAuroraSprite->LoadAuroraSprite("..\\..\\res\\aurora_res\\Boss_Icon_43.sprite");*/

		//	let's rock
		hge->System_Start();

		/*delete pAuroraSprite;
		pAuroraSprite = NULL;*/

		CloseGame();
	}

	// Clean up and shutdown
	hge->System_Shutdown();
	hge->Release();

	return 0;
}
